package com.ruoyi.syf.common.constant;

import java.util.Arrays;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 排查流程相关常量
 *
 * @author yr
 */
public class Check {

    /**
     * 分类每种监测指标的检测项
     * @see CheckInfo
     */
    private static final Map<CheckType, Map<String, CheckInfo>> GROUP_TYPE_MAPPING;
    static {
        GROUP_TYPE_MAPPING = Arrays.stream(CheckInfo.values())
                .collect(Collectors.groupingBy(CheckInfo::getType,
                        Collectors.toMap(CheckInfo::getCode, Function.identity())));
    }

    /**
     * 排查任务跟踪状态类型
     */
    public enum CheckType {
        /** 排查 */
        CHECK(1),
        /** 隐患 */
        DANGER(2),
        /** 整改 */
        FIX(3),
        /** 验收 */
        DETECT(4);

        private int code;
        private CheckType(int code) {
            this.code = code;
        }

        public int getCode() {
            return code;
        }
    }

    /**
     * 排查任务跟踪状态
     */
    public enum CheckInfo {
        /** 排查阶段 */
        C_NO("0", "未排查",CheckType.CHECK),
        C_ALREADY("1", "已排查",CheckType.CHECK),
        C_TO_CONFIRM("2", "待确认",CheckType.CHECK),
        C_NO_ADOPTION("3", "不采纳",CheckType.CHECK),
        C_FORWARD("4", "转派责任人",CheckType.CHECK),
        C_BACK("5", "回退",CheckType.CHECK),
        /** 发现隐患阶段 */
        D_TO_CONFIRM("0", "待确认",CheckType.DANGER),
        D_CONFIRM("1", "已确认",CheckType.DANGER),
        D_NO_ADOPTION("2", "不采纳",CheckType.DANGER),
        /** 整改阶段 */
        F_TO_CONFIRM("0", "待整改",CheckType.FIX),
        F_SUCCEED("1", "已整改",CheckType.FIX),
        F_OVERDUE("2", "超期未整改",CheckType.FIX),
        /** 验收阶段 */
        E_AWAIT("0", "待验收",CheckType.DETECT),
        E_FAIL("1", "验收未通过",CheckType.DETECT),
        E_SUCCEED("2", "验收通过",CheckType.DETECT),
        E_BACK("3", "回退",CheckType.DETECT);

        private String code;
        private String name;
        private CheckType type;

        CheckInfo(String code, String name,CheckType type) {
            this.code = code;
            this.name = name;
            this.type = type;
        }

        /** 根据编码获取排查阶段状态 */
        public static CheckInfo getCheckByCode(String code) {
            return getInfoByCodeAndType(code, CheckType.CHECK);
        }
        /** 根据编码获取发现隐患阶段状态 */
        public static CheckInfo getDangerByCode(String code) {
            return getInfoByCodeAndType(code, CheckType.DANGER);
        }
        /** 根据编码获取整改阶段状态 */
        public static CheckInfo getFixByCode(String code) {
            return getInfoByCodeAndType(code, CheckType.FIX);
        }
        /** 根据编码获取验收阶段状态 */
        public static CheckInfo getDetectByCode(String code) {
            return getInfoByCodeAndType(code, CheckType.DETECT);
        }
        /** 根据编码与类型获取监测指标明细 */
        public static CheckInfo getInfoByCodeAndType(String code, CheckType checkType) {
            if (checkType == null) {
                throw new IllegalArgumentException("监测类型不能为空");
            }
            Map<String, CheckInfo> typeMap = GROUP_TYPE_MAPPING.get(checkType);
            if (typeMap == null) {
                return null;
            } else {
                return typeMap.get(code);
            }
        }

        public static CheckInfo getInfoByCode(String code) {
            CheckInfo result = null;
            for (Map<String, CheckInfo> value : GROUP_TYPE_MAPPING.values()) {
                CheckInfo info = value.get(code);
                if (info != null) {
                    result = info;
                    break;
                }
            }
            return result;
        }

        public String getCode() {
            return code;
        }

        public String getName() {
            return name;
        }

        public CheckType getType() {
            return type;
        }
    }

    public static void checkEqualsStatus(String code, CheckInfo... checkInfo) throws IllegalAccessException {
        if (checkInfo == null || checkInfo.length == 0) {
            return;
        }
        boolean flag = false;
        for (CheckInfo info : checkInfo) {
            if (code.equals(info.getCode())) {
                flag = true;
                break;
            }
        }
        if (!flag){
            throw new IllegalAccessException("排查状态异常!");
        }
    }

}
